
package com.femass.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import com.femass.model.Artigo;
import com.femass.repository.ArtigoRepository;
import com.femass.util.Repositorios;

@FacesConverter(forClass = Artigo.class)
public class ArtigoConverter implements Converter{

    private Repositorios repositorios = new Repositorios();
    private ArtigoRepository artigosRepo = this.repositorios.getArtigos();
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
                
        if(!value.equals("")){
            return (Artigo) this.artigosRepo.porCodigo(new Integer(value));
        }
        
        return null;      
        
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        if(value!=null){
            Integer codigo = ((Artigo)value).getCod();
            return codigo==null ? "" : codigo.toString();
        }
        
        return null;
    }
    
}
